//------ start 填充日期 ---------
var myDate = new Date;
var year = myDate.getFullYear(); //获取当前年
var mon = myDate.getMonth() + 1; //获取当前月
var date = myDate.getDate(); //获取当前日
mon = mon < 10 ? 0 + "" + mon : mon;
date = date < 10 ? 0 + "" + date : date;
$("#checkDate").val(year + "." + mon + "." + date);
//------ end 填充日期 ---------


//选择是自由加载法 还是 自由加速法 后再调整数据
window.onload = function (ev) {
    $("#checkMethod2").text($("#checkMethod").val());
    $("#checkMethod").change(function () {
        $("#checkMethod2").text($(this).val());
        zy_value();
    });
    zy_value();
    limits();
    hg();
};


//start  --自由加速平均值--  --自由检测最大值--
$("#checkResult1").blur(function () {
    zy_value();
});
$("#checkResult2").blur(function () {
    zy_value();
});
$("#checkResult3").blur(function () {
    zy_value();
});
function zy_value() {
    var res1 = $("#checkResult1").text().trim();
    var res2 = $("#checkResult2").text().trim();
    var res3 = $("#checkResult3").text().trim();
    var result = 0;
    if (res1 != "" && res2 != "" && res3 != "") {
        if ("自由加速法" == $("#checkMethod").val()) {
            var fix =  Math.max(res1.substring(res1.indexOf(".")+1).length,res2.substring(res2.indexOf(".")+1).length,res3.substring(res3.indexOf(".")+1).length);
            fix = fix < 2 ? 2 : fix;
            result = ((Number(res1) + Number(res2) + Number(res3)) / 3).toFixed(fix);
        } else {
            result = Math.max(Number(res1), Number(res2), Number(res3));
        }
        $("#accelerateResult").text(result);
    } else {
        $("#accelerateResult").text("");
    }
    //-----start 合格判定
    hg();
    //-----end 合格判定
}

//end  --自由加速平均值--  --自由检测最大值--


//start  -------排放限值------
$("#banBlowdownRegion").change(function () {
    limits();
    //-----start 合格判定
    hg();
    //-----end 合格判定
});
function limits(){
    var bnr = $("#banBlowdownRegion").val();
    var power = Number($("#enginePower").text().trim());
    var period = $("#emissionPeriod input:radio:checked").val();
    var result = "";
    if (bnr == "是") {
        if (power < 37) {
            result = "0.80";
        } else {
            result = "0.50";
        }
        $("#emissionLimits").text(result);
    }else{
        $("#emissionLimits").text(xz(period, power));
    }
}
$("#enginePower").blur(function () {
    var period = $("#emissionPeriod input:radio:checked").val();
    var result;
    var power = Number($("#enginePower").text().trim());
    if($("#banBlowdownRegion").val() == "是"){
        limits();
        hg();
        return false;
    }
    if (power != "") {
        $("#emissionLimits").text(xz(period, power));
    } else {
        $("#emissionLimits").text("");
    }
    //-----start 合格判定
    hg();
    //-----end 合格判定
});
$("#emissionPeriod :radio").on("click", function () {
    var period = $(this).val();
    var power = Number($("#enginePower").text().trim());
    if (power != "") {
        $("#emissionLimits").text(xz(period, power));
    } else {
        $("#emissionLimits").text("");
    }
    //-----start 合格判定
    hg();
    //-----end 合格判定
});

function xz(period, power) {
    var result;
    if (period == "I" || period == "II") {
        if (power < 19) {
            result = "3.00";
        } else if (power < 37) {
            result = "2.00";
        } else if (power <= 560) {
            result = "1.61";
        }
    } else if (period == "III") {
        if (power < 19) {
            result = "2.00";
        } else if (power < 37) {
            result = "1.00";
        } else {
            result = "0.80";
        }
    } else if(period == "X"){
        if (power < 37) {
            result = "0.80";
        } else {
            result = "0.50";
        }
    }else{
        result = "";
    }
    return result;
}

//end  -------排放限值------

//合格判定
$("#visualPlume :radio").on("click", function () {
    hg();
});
function hg() {
    var limit = $("#emissionLimits").text().trim();
    var result = $("#accelerateResult").text().trim();
    var vp = $("#visualPlume input:radio:checked").val();
    var period = $("#emissionPeriod input:radio:checked").val();
    var power = Number($("#enginePower").text().trim());
    var temp = "合格";
    if (limit != "" && result != "") {
        if (result > limit) {
            temp = "不合格";
        } else {
            if (vp == 1) {
                if (period == "II" && power >= 19) {
                    temp = "不合格";
                } else if (period == "III") {
                    temp = "不合格";
                }
            }
        }
        $("#resultJudgment").text(temp);
    } else {
        $("#resultJudgment").text("");
    }
}
$("#save_report").click(function () {
    //异步请求 ,保存
    submitOrSave("/user/submit-report?type=0");
});
$("#submit_report").click(function () {

    //查看是否有未填写的字段
    var con = true;
    $(".report_content").each(function () {
        if ($(this).text().trim() == "") {
            $(this).focus();
            alert("请填写完整数据,空数据请替换成\"/\"  -->");
            con = false;
            return false;
        }
    });
    if (!con) {
        return false;
    }
    // if(!checkSubmitPassword()){
    //     alert("口令有误!提交失败!!!!");
    //     return;
    // }
    //异步请求
    submitOrSave("/user/submit-report");
});
// function checkSubmitPassword() {
//     var uid = localStorage.getItem("token");
//     if(uid == 44){
//         var password =  prompt("请输入口令：");
//         if(password != "12053783"){
//             return false;
//         }
//     }
//     return true;
// }
function submitOrSave(url) {
    //获取填写的图片, 暂时不强制
    var arr = [];
    var count = 1;
    //获取图片base64
    $(".file-input").each(function () {
        try {
            arr[count++] = $(this).find("img")[0].src;
        } catch (e) {
        }
    });
    $.ajax({
        url : url,
        type: "post",
        contentType: "application/json;charset=utf-8",
        beforeSend: function (request) {
            $("#loading").modal("show");
            request.setRequestHeader("uid", localStorage.getItem("token"));
        },
        data:
            JSON.stringify({
                reportId :$("#reportId").text(),
                reportNumber: $("#reportNumber").text().trim(),
                checkDate: $("#checkDate").val(),
                checkMethod: $("#checkMethod").val(), //因为select 所以要获取其val
                checkOrganization: $("#checkOrganization").text().trim(),
                basisStandard: $("#basisStandard").text().trim(),
                organizationAddress: $("#organizationAddress").text().trim(),
                agentName: $("#agentName").text().trim(),
                agentPhone: $("#agentPhone").text().trim(),
                agentAddress: $("#agentAddress").text().trim(),
                equipmentManufacturer: $("#equipmentManufacturer").text().trim(),
                engineManufacturerName: $("#engineManufacturerName").text().trim(),
                serialNumber: $("#serialNumber").text().trim(),
                hbNumber: $("#hbNumber").text().trim(),
                machineProtectionNumber: $("#machineProtectionNumber").text().trim(),
                machineNumber: $("#machineNumber").text().trim(),
                equipmentNumber: $("#equipmentNumber").text().trim(),
                machineName: $("#machineName").text().trim(),
                trademark: $("#trademark").text().trim(),
                productionDate: $("#productionDate").text().trim(),
                equipmentColor: $("#equipmentColor").text().trim(),
                emissionWarranty: $("#emissionWarranty").text().trim(),
                checkBasis: $("#checkBasis").text().trim(),
                machineVin: $("#machineVin").text().trim(),
                engineNumber: $("#engineNumber").text().trim(),
                engineModel: $("#engineModel").text().trim(),
                emissionPeriod: $("#emissionPeriod input:radio:checked").val(),  //因为是radio 获取radio
                engineYear: $("#engineYear").text().trim(),
                enginePower: $("#enginePower").text().trim(),
                engineSpeed: $("#engineSpeed").text().trim(),
                engineOils: $("#engineOils").text().trim(),
                protectionNumber: $("#protectionNumber").text().trim(),
                engineTypeApprovalNumber: $("#engineTypeApprovalNumber").text().trim(),
                machineType: $("#machineType").val(),
                checkType: $("#checkType").val(),
                workCharacteristic: $("#workCharacteristic").val(),
                engineAftertreatmentSystem: $("#engineAftertreatmentSystem").val(),
                banBlowdownRegion: $("#banBlowdownRegion").val(),
                driverAlarmSystem: $("#driverAlarmSystem").val(),
                driverPerformanceRestrictionSystem: $("#driverPerformanceRestrictionSystem").val(),
                ecuInfo: $("#ecuInfo").val(),
                remoteMonitorSystem: $("#remoteMonitorSystem").val(),
                satellitePositionSystem: $("#satellitePositionSystem").val(),
                ambientTemperature: $("#ambientTemperature").text().trim(),
                altitude: $("#altitude").text().trim(),
                ambientHumidity: $("#ambientHumidity").text().trim(),
                atmosphericPressure: $("#atmosphericPressure").text().trim(),
                equipmentName: $("#equipmentName").text().trim(),
                equipmentModel: $("#equipmentModel").text().trim(),
                equipmentFactoryNumber: $("#equipmentFactoryNumber").text().trim(),
                equipmentMeasurementCertificate: $("#equipmentMeasurementCertificate").text().trim(),
                checkStartTime: $("#checkStartTime").text().trim(),
                checkEndTime: $("#checkEndTime").text().trim(),
                checkPlace: $("#checkPlace").text().trim(),
                checkResult1: $("#checkResult1").text().trim(),
                checkResult2: $("#checkResult2").text().trim(),
                checkResult3: $("#checkResult3").text().trim(),
                checkEngineSpeed: $("#checkEngineSpeed").text().trim(),
                visualPlume: $("#visualPlume input:radio:checked").val(),
                accelerateResult: $("#accelerateResult").text().trim(),
                emissionLimits: $("#emissionLimits").text().trim(),
                resultJudgment: $("#resultJudgment").text().trim(),
                fpic1: arr[1],
                fpic2: arr[2],
                fpic3: arr[3],
                fpic4: arr[4],
                fpic5: arr[5],
                fpic6: arr[6],
                fpic7: arr[7],
                fpic8: arr[8],
                fpic9: arr[9],
                fpic10: arr[10]
            }),
        dateType: "json",
        success: function (data) {
            //show 和 hide 如果时间间隔太短的话 , hide会失效 , 所以这里延迟了500ms 处理
            setTimeout(function() {
                $("#loading").modal("hide");
                alert(data.message);
            },500);
        }
    })
}

$("#submit_modify_report").click(function () {
    //查看是否有未填写的字段
    var con = true;
    $(".report_content").each(function () {
        if ($(this).text().trim() == "") {
            $(this).focus();
            alert("请填写完整数据,空数据请替换成\"/\"  -->");
            con = false;
            return false;
        }
    });
    if (!con) {
        return false;
    }
    // if(!checkSubmitPassword()){
    //     alert("口令有误!提交失败!!!!");
    //     return;
    // }
    $.ajax({
        url : "/user/report1/modify-status?rid=" + $("#reportId").text()+"&status=1",
        type: "post",
        beforeSend: function (request) {
            request.setRequestHeader("uid", localStorage.getItem("token"));
        },
        success:function (res) {
            alert(res.message);
        }
    });
});